<!DOCTYPE html>
<html lang="en">
<head>
    <title>管理系统 - 管理中心</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- <meta ng-include="'head.html'"> -->
    <style type="text/css">
        .table .input-group-btn>.btn{ height:30px; margin-top:0px;}
        .table>thead>tr>th{padding-bottom: 0px; font-size: 14px}
        .table tr>td>a{ color:#696969;}
        .table tr>td{ font-size: 14px }
        .opt{ color:#19B5FE; }
        .edit{ color:#428bca; margin-right: 15px }
    </style>
    <link rel="stylesheet" type="text/css" href="../lib/css/footable.bootstrap.min.css">
    <script src="../lib/js/jquery.min.js" type="text/javascript"></script>
    <script src="../js/build.js" type="text/javascript"></script>
    <script src="../js/public.js" type="text/javascript"></script>

</head>
<body class="flat-blue">
<div class="app-container H100" ng-app="myApp" ng-controller="myCtrl">
<div class="row content-container H100">
<div ng-include="'menus.html'" onload="loadReady()"></div>
<div class="container-fluid H100 pb15">
<div class="side-body padding-top H100" id="main" style="padding-top:70px">
    <!------------------Main---------------->
    <div class="H100" style="width:99%; background-color:#fff; position:relative">
        <!--<div class="titleHead">账户安全</div>-->
        <div style="padding: 2px 15px 0px 15px; min-height: 600px; font-size: 16px; background-color: #fff; position: relative">
            <div class="titleHead" style="font-size: 14px">➢公司管理</div>
            <table class="table table-striped" style="padding-bottom: 20px"></table>
            <div style="position:absolute; bottom:25px; right:30px">
                <button style="float: right" ng-click="addNewCompany();" class="bt btn btn-primary" data-toggle="modal" >
                    <span class="glyphicon glyphicon-plus"></span> 创建公司
                </button>
            </div>
        </div>

        <!-- 模态对话框 -->
        <div ng-include="'modalCompany.html'"></div>
        <!-- End · 模态对话框 -->

    </div>
    <!------------------End Main---------------------->
</div>  <!--End side-body-->
</div>  <!--End container-fluid-->
</div>  <!--End content-container-->
<div ng-include="'footer.html'"></div>
</div>  <!--End app-container-->

<script type="text/javascript" src="../js/addCompany.js"></script>
<script type="text/javascript" src="../lib/js/footable.min.js"></script>
<script language="javascript">
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        loadCtlBase($scope);

        // $scope.Addrs = {'address': {"selProvince":"", "selCity":"", "selCounty":""} };
        // loadAddrs($scope, $scope.Addrs);
        $scope.cols = [];
        $scope.data = [];
        $scope.cacheData = {};
        $scope.line = {};
        initCompany($scope);
        loadCompanys("view1");

        // $scope.getNewCompany = function(){
        //     $scope.line = {
        //         id:-1,
        //         companyName: '',
        //         address: '',
        //         editUser:'',
        //         telephone:'',
        //         other:'',
        //     };
        //     initAddr("", "", "");
        //     $scope.cols[0].disabled = false;
        // }

        // function initCols(obj){
        //     $scope.cols = [];
        //     $scope.cols.push({'name':'公司', 'field': 'companyName', 'type':'text', 'disabled':true });
        //     $scope.cols.push({'name':'地址', 'field': 'address', 'type':'address' });
        //     $scope.cols.push({'name':'负责人', 'field': 'editUser', 'type':'text', 'disabled':false });
        //     $scope.cols.push({'name':'联系电话', 'field': 'telephone', 'type':'text', 'disabled':false });
        //     $scope.cols.push({'name':'备注', 'field': 'other', 'type':'text', 'disabled':false });
        // }

        // function initAddr(province, city, county){
        //     $scope.Addrs['address'].selProvince = province;
        //     $scope.Addrs['address'].selCity = city;
        //     $scope.Addrs['address'].selCounty = county;
            
        //     $scope.updateAddrs("address");     //// 地址组件赋值手动赋值完
        // }

        function loadTable(){
            var data = $scope.data;
            $('.table').html("");
            var cols = [{'name':'companyName', 'title':'公司名'}, 
                {'name':'province', 'title':'省'}, 
                {'name':'city', 'title':'城市'}, 
                {'name':'count', 'title':'设备数量', 'visible':false},
                {'name':'telephone', 'title':'联系电话'}, 
                {'name':'editUser', 'title':'负责人'}, 
                {'name':'operate', 'title':'操作'}
            ];
            function getHref(d, text){
                return "<a href='companyInfo.html?cp="+ escape(d.companyName) +"&pvn="
                       + escape(d.province) + "' title='点击查看详情'>" + text + "</a>"
            }
            var rows = data.map(function(d, i){
                $scope.cacheData[d.id] = i;
                return {
                    companyName: getHref(d, d.companyName),
                    province: d.province, city: d.city, editUser:d.editUser,
                    count: getHref(d, d.count), telephone:d.telephone,
                    operate: '<span class="glyphicon glyphicon-edit pointer edit" title="编辑" data-id="'
                            + d.id +'"></span>'
                };
            });
            $('.table').footable({
                "paging": { "enabled": true, 'size':13 },
                "filtering": { "enabled": true },
                "sorting": { "enabled": true },
                "columns": cols,
                "rows": rows
            });
            //// 添加编辑事件
            $('.table').on('click', '.edit', function(){ 
                var id = $(this).attr("data-id");
                $scope.line = $scope.data[$scope.cacheData[id]];
                initAddr($scope, $scope.line.province, $scope.line.city, $scope.line.county);
                $scope.cols[0].disabled = true;
                $scope.$apply();

                $("#myModal1").modal('show');

            });

            //// 提交公司信息
            $scope.setCompany = function() {
                setCompany($scope, function(){
                    if($scope.line.id==-1)$scope.data.unshift($scope.line);
                    loadTable();
                });

                return;
                $scope.line['province'] = $scope.Addrs['address'].selProvince;
                $scope.line['city'] = $scope.Addrs['address'].selCity;
                $scope.line['county'] = $scope.Addrs['address'].selCounty;
                $scope.line['module'] = 'admin'; 
                var dt = {'company':$scope.line.companyName, cp:JSON.stringify($scope.line)};
                
                ajaxData('set_company_info', dt, function(rst){
                    if($scope.line.id==-1){
                        if(rst.data!=-1){
                            $scope.line['id'] = rst.data;
                            $scope.data.unshift($scope.line);
                        }else{
                            alert("修改失败，请检查资料");
                            return;
                        }
                    }else{
                        if(rst.data!=0){
                            alert("修改失败，请检查资料");
                            return;
                        }    
                    }
                    
                    loadTable();
                    $("#myModal1").modal('hide');
                });
            };

        }

        $scope.addNewCompany = function(){
            $scope.getNewCompany();

            $("#myModal1").modal('show');
        }

        function loadCompanys(id){
            ajaxData('get_companys', {}, function(rst){
                var data = rst.data; 
                $scope.data = data;
                loadTable();

            });
        }

});
    

    


</script>

</body>
</html>
